package com.youlai.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.enums.LogModuleEnum;
import com.youlai.system.model.entity.SysCheckItem;
import com.youlai.system.model.entity.SysRegisteredItem;
import com.youlai.system.model.form.CheckItemForm;
import com.youlai.system.model.query.CheckItemPageQuery;
import com.youlai.system.model.query.RegisteredPageQuery;
import com.youlai.system.plugin.syslog.annotation.LogAnnotation;
import com.youlai.system.service.SysRegisteredItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yao
 * @create 2024/8/27 上午11:09
 */
@Tag(name = "挂号接口")
@RestController
@RequestMapping("/api/v1/registered")
@RequiredArgsConstructor
public class SysRegisteredItemController {
    private final SysRegisteredItemService sysRegisteredItemService;

    @Operation(summary = "获取检查项目列表")
    @GetMapping("/page")
    @LogAnnotation(value = "检查项目分页列表", module = LogModuleEnum.USER)
    public PageResult<SysRegisteredItem> listPagedUsers(
            RegisteredPageQuery queryParams
    ) {
        IPage<SysRegisteredItem> result = sysRegisteredItemService.listPagedRegistereds(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "添加挂号项目")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:reg:add')")
    public Result<?> addRegistered(@RequestBody @Valid SysRegisteredItem sysRegisteredItem)
    {
        return Result.judge(sysRegisteredItemService.save(sysRegisteredItem));
    }

    @Operation(summary = "获取检查项目详情配置表单数据")
    @GetMapping("/{reg_item_id}/form")
    public Result<SysRegisteredItem> getRegisteredForm(@Parameter(description = "检查项目ID") @PathVariable Long reg_item_id) {
        SysRegisteredItem sysRegisteredItem = sysRegisteredItemService.getById(reg_item_id);
        return Result.success(sysRegisteredItem);
    }

    @Operation(summary = "修改检查项目")
    @PutMapping("/{reg_item_id}")
    @PreAuthorize("@ss.hasPerm('sys:reg:edit')")
    public Result updateRegistered(@PathVariable Long reg_item_id, @RequestBody @Valid SysRegisteredItem reg) {
        SysRegisteredItem registeredItem = new SysRegisteredItem();
        registeredItem.setRegItemId(reg_item_id);
        registeredItem.setRegItemName(reg.getRegItemName());
        registeredItem.setRegItemFee(reg.getRegItemFee());
        registeredItem.setStatus(reg.getStatus());
        return Result.judge(sysRegisteredItemService.updateById(registeredItem));
    }

    @Operation(summary = "查询启用检查项目")
    @GetMapping("/enabled")
    public Result<List<SysRegisteredItem>> getEnabledCheckItem() {
        return Result.success(sysRegisteredItemService.getEnabledCheckItem());
    }
}
